package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.enums.AppointmentEnums;
import cn.wolfcode.qo.AppointmentQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private IBusinessService businessService;

    @RequiredPermission(name="预约页面",expression="appointment:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") AppointmentQueryObject qo){
//        分页回显
        PageInfo<Appointment> pageInfo = appointmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
//        门店选择
        model.addAttribute("business",businessService.listAll());
//        业务大类选择
        model.addAttribute( "sdi",systemDictionaryItemService.getForTypeId(1L)) ;

//
        model.addAttribute("appointmentEnums", AppointmentEnums.values());
        return "/appointment/list";
    }


    @RequiredPermission(name="预约删除",expression="appointment:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           appointmentService.delete(id);
        }
        return "redirect:/appointment/list";
    }


    @RequiredPermission(name="预约新增/编辑",expression="appointment:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Appointment appointment) {

        if (appointment.getId() == null) {
            //        设置订单状态(这是后台传入,按需求改成履行中 1 )
            appointment.setStatus(AppointmentEnums.PERFORM.getValue());
            appointmentService.save(appointment);
        } else {
            appointmentService.update(appointment);
        }
        return "redirect:/appointment/list";
    }

    /**
     *
     * @param appointment
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save(Appointment appointment){
        JsonResult jsonResult = new JsonResult();
        //        设置订单状态(这是后台传入,按需求改成0 )
        appointment.setStatus(AppointmentEnums.PEND.getValue());
        try {
            appointmentService.save(appointment);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMsg("插入失败");
        }
        return jsonResult;
    }

    @RequestMapping("/updateStatus")
    public String updateStatus(Integer status,Long id){


            appointmentService.updateStatus(status,id);

        return "redirect:/appointment/list" ;
    }
}
